package com.getpebble.android.framework.endpoint;

import com.getpebble.android.bluetooth.protocol.ProtocolMessage;
import com.getpebble.android.framework.protocol.EndpointId;
import com.getpebble.android.framework.protocol.inbound.PblInboundVersionMessage;
import com.getpebble.android.framework.protocol.outbound.PblOutboundVersionMessage;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

class VersionEndpoint extends Endpoint {
    private static final String TAG = VersionEndpoint.class.getSimpleName();
    private static final Set<EndpointId> sIds = Collections.unmodifiableSet(EnumSet.of(EndpointId.VERSIONS));
    private final Listener mListener;
    private IEndpointMessageSender mMessageSender;

    interface Listener {
        void onReceive(PblInboundVersionMessage pblInboundVersionMessage);
    }

    VersionEndpoint(IEndpointMessageSender messageSender, Listener listener) {
        if (messageSender == null) {
            throw new IllegalArgumentException("'messageSender' cannot be null!");
        }
        this.mMessageSender = messageSender;
        this.mListener = listener;
    }

    private Listener getListener() {
        return this.mListener;
    }

    private IEndpointMessageSender getMessageSender() {
        return this.mMessageSender;
    }

    Set<EndpointId> getSupportedEndpoints() {
        return sIds;
    }

    boolean sendMessage() {
        return getMessageSender().sendMessage(new PblOutboundVersionMessage());
    }

    boolean onReceive(ProtocolMessage message) {
        if (message.getEndpointId() != EndpointId.VERSIONS.getCode()) {
            return false;
        }
        PblInboundVersionMessage versionMessage = new PblInboundVersionMessage(message);
        Listener listener = getListener();
        if (listener != null) {
            listener.onReceive(versionMessage);
        }
        return true;
    }
}
